/***
 * * PROJECT
 * *    Logical Neighborhoods - J2ME Run-Time Support 
 * * VERSION
 * *    $LastChangedRevision: 118 $
 * * DATE
 * *    $LastChangedDate: 2008-05-28 19:35:12 +0200 (Wed, 28 May 2008) $
 * * LAST_CHANGE_BY
 * *    $LastChangedBy: lmottola $
 * *
 * *    $Id: AppMsg.java 118 2008-05-28 17:35:12Z lmottola $
 * *
 * *   Logical Neighborhoods - 
 * *                   Programming Wireless Sensor Networks
 * *                   (One Slice at a Time)
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU Lesser General Public License
 * *   as published by the Free Software Foundation; either version 2.1
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU Lesser General Public 
 * *   License along with this program; if not, you may find a copy at 
 * *   the FSF web site at 'www.gnu.org' or 'www.fsf.org', or you may 
 * *   write to the Free Software Foundation, Inc., 59 Temple 
 * *   Place - Suite 330, Boston, MA  02111-1307, USA
 ***/

package atag.runtime.ln.messages;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;


/**
 * @author Luca Mottola <a
 *         href="mailto:mottola@elet.polimi.it">mottola@elet.polimi.it</a>
 * 
 */
public class ReplyMsg extends LNMessage {

	private int destination;

	private byte[] data;

	public ReplyMsg(int originatorId, long seqNumber, byte[] inData,
			int destination) {

		super(originatorId, seqNumber);
		this.destination = destination;
		this.data = new byte[inData.length];
		for (int i = 0; i < inData.length; i++) {
			this.data[i] = inData[i];
		}
	}

	public byte[] getData() {
		return data;
	}

	public int getDestination() {
		return destination;
	}

	public DataOutput serialize(DataOutput dg) throws IOException {

		dg.writeByte(LNMessage.REPLY_MSG_TYPE);
		dg = super.serialize(dg);

		dg.writeInt(destination);
		dg.writeInt(data.length);
		for (int i = 0; i < data.length; i++) {
			dg.writeByte(data[i]);
		}

		return dg;
	}

	public static ReplyMsg deserialize(DataInput dg, int senderId,
			int originatorId, long seqNum, byte hops) throws IOException {

		int destination = dg.readInt();
		int dataLength = dg.readInt();
		byte[] data = new byte[dataLength];
		for (int i = 0; i < data.length; i++) {
			data[i] = dg.readByte();
		}
		ReplyMsg replyMsg = new ReplyMsg(originatorId, seqNum, data,
				destination);
		replyMsg.setSenderId(senderId);
		replyMsg.setHops(hops);
		return replyMsg;
	}

}
